import 'package:gameplay_flutter/m_room/model/room_data_model.dart';
import 'package:gameplay_flutter/m_room/room_api.dart';
import 'package:gameplay_flutter/m_room/room_main_page.dart';
import 'package:gameplay_flutter/m_rtc/agora_rtc_strategy.dart';
import 'package:gameplay_flutter/m_rtc/rtc_live_strategy.dart';
import 'package:gameplay_flutter/m_rtm/rtm_strategy.dart';
import 'package:get/get.dart';

import '../lib_network/http_helper.dart';
import '../m_rtm/agora_rtm_strategy.dart';
import 'model/room_base_model.dart';
import 'room_chat/model/room_message_event.dart';
import 'model/token_model.dart';
import 'room_main_controller.dart';

class RoomManager {
  // 1. 私有静态实例
  static final RoomManager _instance = RoomManager._internal();

  // 2. 私有构造函数
  RoomManager._internal();

  // 3. 工厂构造函数返回单例
  factory RoomManager() => _instance;

  // 新增静态获取方法
  static RoomManager instance = _instance;

  late RtcLiveStrategy rtcLiveStrategy;
  late RtmStrategy rtmStrategy;


  String? roomTokenBean;
  TokenModel? tokenModel;

  RoomMainController controller = Get.put(RoomMainController());

  Future<bool> initSdk() async {
    rtcLiveStrategy = AgoraRtcStrategy();
    rtmStrategy = AgoraRtmStrategy();
    if (tokenModel != null) {
      await rtcLiveStrategy.initializeSdk();
      await rtmStrategy.initializeSdk(tokenModel?.rtcToken);
      return Future.value(true);
    }else{
      return Future.value(false);
    }
  }

  void joinChannel(String channelId,void Function(RoomMessageEvent event)? message) async {
    if (tokenModel != null) {
      await rtcLiveStrategy.joinChannel(channelId, tokenModel?.rtcToken??"");
      print('Room RTM start subscribe channel: ${channelId}');
      await rtmStrategy.subscribe(channelId,message);
    }
  }

  void leaveChannel(String channelId) async {
    await rtcLiveStrategy.leaveChannel();
    await rtmStrategy.unSubscribe(channelId);
  }

  void switchRole(bool isRoleBroadcaster) async {
    await rtcLiveStrategy.switchRole(isRoleBroadcaster);
  }

  void send(String channelId) async {
    await rtmStrategy.publish(channelId);
  }

  void dispose() async {
    tokenModel = null;
    await rtcLiveStrategy.dispose();
    await rtmStrategy.dispose();
  }

  void joinRoom(String roomNo) {
    enterExt(roomNo);
  }

  void enterExt(
      String roomNo,
      {String password = "",}) async {
    String timestamp = DateTime.now().millisecondsSinceEpoch.toString();
    var res = await HttpHelper.instance().post(
      RoomApi.enterExt,
      body: {
        "roomNo": roomNo,
        "password": password,
        "timestamp": timestamp,
        "followUserId": "",
        "pushOrderMsgId": "",
      },
      showLoading: true,
      showToast: true,
    );
    if (res.result is Map<String, dynamic>) {
      RoomDataModel roomDataModel = RoomDataModel.fromJson(res.result);
      if (roomDataModel.roomInfo != null &&
          roomDataModel.userInfo != null &&
          roomDataModel.roomTokenBean != null) {
        tokenModel = TokenModel.fromJson(roomDataModel.tokenMap);
        controller.addNewRoom(roomDataModel as RoomDataModel);
        Get.to(() => const RoomMainPage());
      }
    }
  }

  void enterBase(
    String roomNo, {
    String password = "",
  }) async {
    String timestamp = DateTime.now().millisecondsSinceEpoch.toString();
    var res = await HttpHelper.instance().post(
      RoomApi.enterBase,
      body: {
        "roomNo": roomNo,
        "timestamp": timestamp,
        "password": password,
        "isFromEditRoom": true,
      },
      showLoading: true,
      showToast: true,
    );
    if (res.result is Map<String, dynamic>) {
      RoomBaseModel roomBaseModel = RoomBaseModel.fromJson(res.result);
      if (roomBaseModel.roomTokenBean != null &&
          roomBaseModel.roomBaseInfo != null) {
        roomTokenBean = roomBaseModel.roomTokenBean;
        // controller.addNewRoom(roomBaseModel.roomBaseInfo!);
        Get.to(() => const RoomMainPage());
      }
    }
  }
}
